package com.lock.subscribe.util;

import com.alibaba.fastjson.JSONObject;

import java.util.Calendar;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;

public class QrCodeUtil {
	 public static String getDeviceQrData(String mac ,String baseCode, long id,String startTime,String endTime) {
	        // 根据设备MAC BASECODE 获取加密密码
	        String qrPassWord = getQrPassWord(mac, baseCode);
	        if (qrPassWord == null) {
	            return qrPassWord;
	        }

	        String password = String.valueOf(qrPassWord);

//	        Calendar cd = Calendar.getInstance();
//	        //获取开始时间+8
//	        cd.add(Calendar.HOUR, 8);
//	        long l = cd.getTime().getTime() / 1000;
//	        //获取二维码有效期
//	        cd.add(Calendar.MINUTE, 30);
//	        //设置结束时间
//	        long l1 = cd.getTime().getTime() / 1000;
			startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
			endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";

	        StringBuilder sb = new StringBuilder("AA0000");
	        // 获取16进制用户ID 4字节
	        String hexuId = getHexuId(id);
	        if (hexuId == null) {
	            return hexuId;
	        }

	        sb.append(hexuId);
	        // 时间加8H
	        sb.append(HexUtils.toHexL(startTime, 8));
	        sb.append(HexUtils.toHexL(endTime, 8));

	        byte[] bytes = HexUtils.hexStringTobytes(sb.toString());
	        int total = 0;
	        for (byte aByte : bytes) {
	            total = total + Byte.toUnsignedInt(aByte);
	        }
	        // 校验位 从二维码类型到结束时间之和&0xFF
	        String s1 = Integer.toHexString(total & 0xFF);
	        if (s1.length() < 2) {
	            sb.append("0");
	        }

	        sb.append(s1);
	        String s = sb.toString().toUpperCase(Locale.ROOT);
	        String data = null;
	        try {
	            data = EncryptStrUtil.encrypt(password, s);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        StringBuilder r = new StringBuilder("5AAA0010").append(data.toUpperCase(Locale.ROOT)).append("00A5");
	        JSONObject result = new JSONObject();
	        result.put("data", r.toString());
	        result.put("startTime",startTime);
	        result.put("endTime",endTime);
	        result.put("passwordId",hexuId);
	        return result.toJSONString();
	    }
    public String getDeviceQrData(String mac ,String baseCode, Long id) {
        // 根据设备MAC BASECODE 获取加密密码
        String qrPassWord = getQrPassWord(mac, baseCode);
        if (qrPassWord == null) {
            return qrPassWord;
        }

        String password = String.valueOf(qrPassWord);

        Calendar cd = Calendar.getInstance();
        //获取开始时间+8
        cd.add(Calendar.HOUR, 8);
        long l = cd.getTime().getTime() / 1000;
        //获取二维码有效期
        cd.add(Calendar.MINUTE, 30);
        //设置结束时间
        long l1 = cd.getTime().getTime() / 1000;


        StringBuilder sb = new StringBuilder("AA0000");
        // 获取16进制用户ID 4字节
        String hexuId = getHexuId(id);
        if (hexuId == null) {
            return hexuId;
        }

        sb.append(hexuId);
        // 时间加8H
        sb.append(Long.toHexString(l)).append(Long.toHexString(l1));

        byte[] bytes = HexUtils.hexStringTobytes(sb.toString());
        int total = 0;
        for (byte aByte : bytes) {
            total = total + Byte.toUnsignedInt(aByte);
        }
        // 校验位 从二维码类型到结束时间之和&0xFF
        String s1 = Integer.toHexString(total & 0xFF);
        if (s1.length() < 2) {
            sb.append("0");
        }

        sb.append(s1);
        String s = sb.toString().toUpperCase(Locale.ROOT);
        String data = null;
        try {
            data = EncryptStrUtil.encrypt(password, s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuilder r = new StringBuilder("5AAA0010").append(data.toUpperCase(Locale.ROOT)).append("00A5");
        JSONObject result = new JSONObject();
        result.put("data", r.toString());
        result.put("startTime", l);
        result.put("endTime", l1);
        return result.toJSONString();
    }


    // 获取加解密的密码
    private static String getQrPassWord(String deviceMac, String baseCode) {

        if (StringUtils.isBlank(deviceMac)) {
            return null;
        }
        if (StringUtils.isBlank(baseCode)) {
            return  null;
        }
        // 去掉符号 :
        deviceMac = deviceMac.replaceAll(":", "");
        // 截取后八位
        if (deviceMac.length() > 8) {
            deviceMac = deviceMac.substring(deviceMac.length() - 8);
        }

        StringBuilder sb = new StringBuilder();
        // 蓝牙密码转化为16进制字符串
        String s = Long.toHexString(Long.valueOf(baseCode));
        sb.append(s);
        int i1 = 8 - s.length();
        if (i1 > 0) {
            for (int i2 = 0; i2 < i1; i2++) {
                sb.insert(0, "0");
            }
        }
        sb.append(deviceMac);
        int num = 32 - sb.length();
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                sb.append("0");
            }
        }
        return sb.toString().toUpperCase(Locale.ROOT);
    }


    // 获取16进制用户ID
	private static String getHexuId(long cmdContent) {
        if (cmdContent ==0) {
            return null;
        }
		String i1 =Long.toHexString(cmdContent);
        return  StringUtils.leftPad(i1, 8, "0");
    }
	
	public static void main(String[] args) {
		System.out.println(getDeviceQrData("D6:B6:3F:FD:63:E2","39077947",1717092888l,"2024-05-30 18:11:00","2024-08-28 18:11:00"));
	}
}
